package com.aos.controller;

import com.aos.dto.parameters.UserParameter;
import com.aos.dto.parameters.UserRegisterParameter;
import com.aos.permission.RequiredPermission;
import com.aos.properties.APMSProperties;
import com.aos.service.user.UserService;
import com.aos.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName UserController
 * @Description:用户控制器
 * @Author wumingwang
 * @Date 2018/9/7 16:06
 * @Version 1.0
 */
@Slf4j
@RestController
@Api(value = "User Controller", tags = {"用户接口"})
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private APMSProperties aPMSProperties;

    /**
     * 分页查询用户
     * @param name
     * @param username
     * @param state
     * @param startDate
     * @param endDate
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "查询用户", notes = "分页查询用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "username", value = "账号",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "用户状态",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startDate", value = "用户创建起始时间",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endDate", value = "用户创建结束时间",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "页码", required = true, paramType = "query", dataType = "int" ,example = "1"),
            @ApiImplicitParam(name = "limit", value = "页面大小", required = true, paramType = "query", dataType = "int",example = "10"),
    })
    @RequiredPermission
    @GetMapping("/user/v1/queryByPage")
    public Object queryByPage(String name,String username,String state,String startDate,String endDate,int page, int limit){
        Map<String,Object> map = new HashMap<>();
        map.put("name",name);
        map.put("username",username);
        map.put("state",state);
        map.put("startDate",startDate);
        if(!StringUtils.isEmpty(endDate)){
            endDate = endDate + " 23:59:59";
        }
        map.put("endDate",endDate);
        map.put("pageNum",page);
        map.put("pageSize",limit);
        return userService.queryByPage(map);
    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    @ApiOperation(value = "新增用户", notes = "新增用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "父账号ID", dataType = "Long",example = "0"),
            @ApiImplicitParam(name = "username", value = "登录名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码(可不填，默认123456)", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "用户名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "email", value = "邮箱", dataType = "String"),
            @ApiImplicitParam(name = "mobile", value = "手机号码", dataType = "String"),
            @ApiImplicitParam(name = "sex", value = "性别【男:1;女:2;未知:3】", dataType = "String"),
            @ApiImplicitParam(name = "appCode", value = "应用code", required = true, dataType = "String"),
            @ApiImplicitParam(name = "group", value = "用户所在组织", dataType = "String"),
            @ApiImplicitParam(name = "comment", value = "备注", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "用户状态【1:可用；2不可用】",required = true, dataType = "String"),
            @ApiImplicitParam(name = "roles", value = "角色数组[{appId:[roleIds]}]", required = true, dataType = "List<Map<Long,Long[]>>"),
    })
    @RequiredPermission
    @PostMapping("/user/v1/add")
    public Object addUser(@Validated @RequestBody UserParameter user){
        if(CollectionUtils.isEmpty(user.getRoles())){
            Result.error("角色不能为空！");
        }
        return userService.addUser(user);
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @ApiOperation(value = "删除用户", notes = "删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID",  required = true, dataType = "Long",example = "0"),
    })
    @RequiredPermission
    @DeleteMapping("/user/v1/delete")
    public Object deleteUser(Long id){
        if(id == null){
            return Result.error("用户ID不能为空！");
        }
        return userService.updateState(id,aPMSProperties.getUser().getStateDelete());
    }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @ApiOperation(value = "更新用户", notes = "更新用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID",  required = true, dataType = "Long", example = "0"),
            @ApiImplicitParam(name = "parentId", value = "父账号ID", dataType = "Long",example = "0"),
            @ApiImplicitParam(name = "username", value = "登录名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码（为空时不修改密码）", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "用户名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "email", value = "邮箱", dataType = "String"),
            @ApiImplicitParam(name = "mobile", value = "手机号码", dataType = "String"),
            @ApiImplicitParam(name = "sex", value = "性别【男:1;女:2;未知:3】", dataType = "String"),
            @ApiImplicitParam(name = "appCode", value = "应用code", required = true, dataType = "String"),
            @ApiImplicitParam(name = "group", value = "用户所在组织", dataType = "String"),
            @ApiImplicitParam(name = "comment", value = "备注", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "用户状态【1:可用；2不可用】",required = true, dataType = "String"),
            @ApiImplicitParam(name = "roles", value = "角色数组[{appId:[roleIds]}]", required = true, dataType = "List<Map<Long,Long[]>>"),
    })
    @RequiredPermission
    @PutMapping("/user/v1/update")
    public Object updateUser(@Validated @RequestBody UserParameter user){
        if(user.getId() == null){
            return Result.error("用户ID不能为空！");
        }
        if(CollectionUtils.isEmpty(user.getRoles())){
            Result.error("角色不能为空！");
        }
        return userService.updateUser(user);
    }

    /**
     * 查询用户详情
     * @param id
     * @return
     */
    @ApiOperation(value = "查询用户详情", notes = "查询用户详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID",  dataType = "Long",example = "0"),
    })
    @RequiredPermission
    @GetMapping("/user/v1/queryDetails")
    public Object queryDetails(Long id){
//        if(id == null){
//            return Result.error("用户ID不能为空！");
//        }
        return userService.queryDetails(id);
    }

    /**
     * 冻结/解冻用户
     * @param map
     * @return
     */
    @ApiOperation(value = "冻结/解冻用户", notes = "冻结/解冻用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID",  required = true, dataType = "Long", example = "0"),
            @ApiImplicitParam(name = "state", value = "用户状态【1:可用；2不可用】",required = true, dataType = "String"),
    })
    @RequiredPermission
    @PutMapping("/user/v1/frozenThawing")
    public Object frozenThawing(@RequestBody Map<String,String> map){
        if(StringUtils.isEmpty(map.get("userId"))){
            return Result.error("用户ID不能为空！");
        }
        if(StringUtils.isEmpty(map.get("state"))){
            return Result.error("状态不能为空！");
        }
        Long userId = Long.parseLong(map.get("userId"));
        String state = map.get("state");
        return userService.frozenThawing(userId,state);
    }

    /**
     * 获取用户所有权限,内部服务调用
     * @param userId
     * @return
     */
    @ApiOperation(value = "获取用户权限", notes = "获取用户权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID",  required = true, dataType = "Long",example = "0"),
    })
    @GetMapping("/user/v1/getPermissions")
    public Object getPermissions(Long userId){
        if(userId == null){
            return Result.error("用户ID不能为空！");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        return userService.getPermissionsByAppAndUser(map);
    }

    /**
     * 注册用户
     * @param user
     * @return
     */
    @ApiOperation(value = "注册用户", notes = "注册用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "登录名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码(可不填，默认123456)", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "用户名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "email", value = "邮箱", dataType = "String"),
            @ApiImplicitParam(name = "mobile", value = "手机号码", dataType = "String"),
            @ApiImplicitParam(name = "sex", value = "性别【男:1;女:2;未知:3】", dataType = "String"),
            @ApiImplicitParam(name = "appCode", value = "应用code", required = true, dataType = "String"),
            @ApiImplicitParam(name = "group", value = "用户所在组织", dataType = "String"),
            @ApiImplicitParam(name = "comment", value = "备注", dataType = "String"),
    })
    @PostMapping("/user/v1/register")
    public Object registerUser(@Validated @RequestBody UserRegisterParameter user){
        return userService.registerUser(user);
    }
}
